package com.chenfwind.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.chenfwind.domain.User;
import com.chenfwind.service.UserService;

/****
 * 用户登录Controller
 * 
 * @author Swinglife
 * 
 */
@Controller
public class LoginController {
	
	@Autowired
	private UserService userService;
	
	/**
	 * 跳转到登录页面
	 */
	@RequestMapping(value = "toLogin")
	public String toLogin(){
		//跳转到/page/login.jsp页面
		return "login";
	}
	
	@RequestMapping(value= "loginddd" ,method = RequestMethod.POST)
	public String Login(String account , String password){
		User user = (User) userService.getUserByAccount(account);
		if( user == null){
			return "error";
		}
		if( !user.getPassword().equals(password)){
			return "error";
		}
		//登录后存放进shiro token
		UsernamePasswordToken token = new UsernamePasswordToken(user.getAccount(),user.getPassword());
		Subject subject = SecurityUtils.getSubject();
		subject.login(token);
		System.out.println("登录成功");
		return "redirect:/home";
	}
	
}
